package com.yupi.oj.ojMQ;

import com.rabbitmq.client.Channel;
import com.yupi.oj.judge.JudgeService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@Slf4j
public class OjMessageConsumer {
    @Resource
    private JudgeService judgeService;

//指定监听的消息队列和确认机制
    @SneakyThrows
    @RabbitListener(queues = {Ojconstant.QUEUE_NAME},ackMode = "MANUAL")
    public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long delivery_tag){
        log.info("接受消息={}",message);
        long questionSubmitId = Long.parseLong(message);
        try {
            judgeService.doJudge(questionSubmitId);
            channel.basicAck(delivery_tag,false);
        } catch (IOException e) {
            channel.basicNack(delivery_tag,false,false);
        }
    }
}
